package base.widgets;

import org.redr.canvas.OSPath;
import org.redr.canvas.RRW;
import org.redr.canvas.RedRApplication;
import org.redr.widgetGUI.core.ComboBox;
import org.redr.widgetGUI.core.CommitButton;
import org.redr.widgetGUI.settings.WidgetSettings;

import base.signals.RDataFrameInput;
import base.signals.RLMInput;
import base.signals.RMatrixOutput;

public class LMPredict extends RRW {
	public static final String name = "Linear Model Predict";
	public static final String summary = "Generates a Linear Model Prediction";
	public static final String details = "This widget allows access to the R lm function.  This is used to generate a linear model.";
	public static final String icon = OSPath.join(RedRApplication.parentDir, "coreIcons", "default.png").getAbsolutePath();
	
	private ComboBox intervalCombo;
	private ComboBox typeCombo;
	private CommitButton commitButton;

	public LMPredict(WidgetSettings ws) {
		super(ws);
		
	}

	@Override
	protected void initSignals() {
		this.addInput(new RDataFrameInput(this, "id2", _("Data Frame"), false));
		this.addInput(new RLMInput(this, "id1", _("Fitted Linear Model"), false));
		this.addOutput(new RMatrixOutput(this, "id1", _("Predication Matrix")));
	}

	@Override
	protected void initGUI() {
		this.intervalCombo	=	new ComboBox(this.gui.controlArea, (WidgetSettings) this.settings.getSettings("invervalCombo")
				.setSetting("label", "Interval"));
		this.intervalCombo.addItem("none", "None");
		this.intervalCombo.addItem("confidence", "Confidence");
		this.intervalCombo.addItem("prediction", "Prediction");
		
		this.typeCombo		=	new ComboBox(this.gui.controlArea, (WidgetSettings) this.settings.getSettings("typeCombo")
				.setSetting("label", "Type"));
		this.typeCombo.addItem("response", "Response");
		this.typeCombo.addItem("terms", "Terms");
		
		this.commitButton	=	new CommitButton(this.gui.controlArea, (WidgetSettings) this.settings.getSettings("commitButton")
				.setSetting("label", "Commit"));
		
		
	}

	@Override
	protected void initVariables() {
		this.rvarSettings.setDefault("lmpredict", this.makeVarName("lmpredict"));
		
	}

	@Override
	protected void dataInput(String id, Object data, String widgetId) {
		if ( id.equals("id1") ){
			if (data == null){
				this.settings.setSetting("fitdata", "");
			} else {
				this.settings.setSetting("fitdata", (String) data);
				if ( this.commitButton.processOnInput() ){
					this.commitFunction();
				}
			}
		} else if (id.equals("id2") ){
			if (data == null){
				this.settings.setSetting("testdata", "");
			} else {
				this.settings.setSetting("testdata", (String) data);
				if ( this.commitButton.processOnInput() ){
					this.commitFunction();
				}
			}
		}
		
	}

	private void commitFunction() {
		try{
			String command;
			if ( this.settings.getSetting("testdata").equals("") ){
				// add the test data to the function
				command = String.format(
						"%s<-predict(%s, interval = \"%s\", type = \"%s\")", 
						this.rvarSettings.getSetting("lmpredict"),
						this.settings.getSetting("fitdata"),
						this.intervalCombo.currentId(),
						this.typeCombo.currentId());
			} else {
				// predict on the model itself
				command = String.format(
						"%s<-predict(%s, %s, interval = \"%s\", type = \"%s\")", 
						this.rvarSettings.getSetting("lmpredict"),
						this.settings.getSetting("fitdata"),
						this.settings.getSetting("testdata"),
						this.intervalCombo.currentId(),
						this.typeCombo.currentId());
			}
			this.nullR(command);
		} catch(Exception e){
			e.printStackTrace();
			this.setStatus(2, "Error in running R command\\n" + e.toString());
		}
		
	}

}
